package lab03;

import java.sql.*;

public class Test13 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement  statement = null;
        ResultSet resultSet = null;

        String sql ="insert into"+" t_students(real_name,age,gender,department,stu_no)"+
        "values('刘浏览',18,1,'信息与工程学院','1606101014')";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root","113223");

            statement = connection.createStatement();
            //插入数据
            statement.execute(sql);
            //查询所有学生成绩，并打印
           resultSet = statement.executeQuery("SELECT * FROM t_students");
           while(resultSet.next()){
               System.out.println(resultSet.getString(1));
               System.out.println(resultSet.getString(2));
               System.out.println(resultSet.getString(3));
           }
           //删除id为5的学生信息
            statement.executeUpdate("DELETE FROM t_students WHERE id = 5");
           //修改学生信息
            statement.executeUpdate("UPDATE TABLE t_students SET department = '信息与工程系' WHERE id = 3");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
